from html.parser import HTMLParser
from urllib.request import urlopen
from contextlib import closing

class MyHTMLParser(HTMLParser):
    def __init__(self):
        self.time = []
        self.location = []
        self.title = []
        self.sign = ''
        self.isShow = False
        super().__init__()

    def handle_starttag(self, tag, attrs):
        #print(attrs)
        if attrs:
            if attrs[0][1] == 'event-title':
                self.sign = 'title'
            elif tag == 'time':
                self.sign = 'time'
            elif attrs[0][1] == 'event-location':
                self.sign = 'location'

    def handle_data(self, data):
        print(data)
        if data == 'Upcoming Events':
            self.isShow = True
        elif data == 'You just missed...':
            self.isShow = False
        if self.sign == 'time' and self.isShow:
            self.time.append(data)
            self.sign = ''
        elif self.sign == 'location' and self.isShow:
            self.location.append(data)
            self.sign = ''
        elif self.sign == 'title' and self.isShow:
            self.title.append(data)
            self.sign = ''

parser = MyHTMLParser()
with closing(urlopen('https://www.python.org/events/python-events/')) as page:
    for line in page:
        parser.feed(str(line))

for i in range(len(parser.time)):
    print('-------------------------------------')
    print('地点:%s\n时间:%s\n主题:%s'% (parser.location[i],parser.time[i],parser.title[i]))
    if i == len(parser.time)-1:
        print('-------------------------------------')